﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.Design
{
    /// <summary>
    /// 0, 1, 2,
    /// 3, 4, 5,
    /// 6, 7, 8,
    /// </summary>
    /// <param name="posIndex"></param>
    /// <param name="pointInCanvas">发生事件时鼠标相对于画布的位置</param>
    /// <param name="offset"></param>
    public delegate void SizePointPress(int posIndex, Point pointInCanvas, Point offset);

    public delegate void SwitchAreaEventHandler(UI.AreaPart area);

    /// <summary>
    /// Interaction logic for DesignBorder.xaml
    /// </summary>
    public partial class DesignBorder : UserControl
    {
        public event SizePointPress SizePointPress;
        public event SwitchAreaEventHandler SwitchArea;
        private UI.Part _contentPart;

        public DesignBorder()
        {
            InitializeComponent();

            this._ctlBtnEnter.Visibility = System.Windows.Visibility.Hidden;
        }

        public void SetTargetPart(UI.Part part)
        {
            this._ctlPartCenter.Width = part.XWidth;
            this._ctlPartCenter.Height = part.XHeight;
            this._contentPart = part;
            //if (part is AreaPart)
            //{
            //    this._ctlBtnEnter.Visibility = System.Windows.Visibility.Visible;
            //}
            //else
            //{
            //    this._ctlBtnEnter.Visibility = System.Windows.Visibility.Hidden;
            //}
        }

        //protected override void OnMouseMove(MouseEventArgs e)
        //{
        //    Debug.WriteLine("mouse move");

        //    base.OnMouseMove(e);
        //}

        public void SetOffset(Point borderOffset)
        {
            //Debug.WriteLine("design border set offset:{0},{1}", borderOffset.X, borderOffset.Y);
            Canvas.SetLeft(_ctlBorder, borderOffset.X - 5);
            Canvas.SetTop(_ctlBorder, borderOffset.Y - 5);
        }

        public void SetSize(Size size)
        {
            this._ctlPartCenter.Width = size.Width;
            this._ctlPartCenter.Height = size.Height;
        }

        private void _ctlPartLeftTop_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            // DesignBorder和画布的大小一致，所以可以用designborder里的相对位置来表示画布里的相对位置
            Point pointInCanvas = e.GetPosition(this._ctlCanvas);

            if (SizePointPress != null)
            {
                if (sender == this._ctlPartLeftTop)
                {
                    SizePointPress(0, pointInCanvas, new Point(0, 0));
                }
                else if (sender == this._ctlPartTop)
                {
                    SizePointPress(1, pointInCanvas, new Point(0, 0));
                }
                else if (sender == this._ctlPartRightTop)
                {
                    SizePointPress(2, pointInCanvas, new Point(0, 0));
                }
                else if (sender == this._ctlPartLeft)
                {
                    SizePointPress(3, pointInCanvas, new Point(0, 0));
                }
                else if (sender == this._ctlPartCenter)
                {
                    SizePointPress(4, pointInCanvas, e.GetPosition(_ctlPartCenter));
                }
                else if (sender == this._ctlPartRight)
                {
                    SizePointPress(5, pointInCanvas, new Point(0, 0));
                }
                else if (sender == this._ctlPartLeftBottom)
                {
                    SizePointPress(6, pointInCanvas, new Point(0, 0));
                }
                else if (sender == this._ctlPartBottom)
                {
                    SizePointPress(7, pointInCanvas, new Point(0, 0));
                }
                else if (sender == this._ctlPartRightBottom)
                {
                    SizePointPress(8, pointInCanvas, new Point(0, 0));
                }
                e.Handled = true;
            }
        }

        private void _ctlBtnEnter_Click(object sender, RoutedEventArgs e)
        {
            if (SwitchArea != null)
            {
                SwitchArea((UI.AreaPart)this._contentPart);
            }
        }
    }
}
